package encome.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import encome.work.dao.Car;
import encome.work.dao.ResponseResult;
import encome.work.utils.StatusCode;
import encome.work.service.CarService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/car")
public class CarController {
    @Resource
    private CarService carService;

    /**
     * 新增车辆
     */
    @PostMapping("/insert")
    public ResponseResult<String> add(@RequestBody Car car){
        carService.add(car);
        return new ResponseResult<>(StatusCode.OK, "新增车辆信息成功");
    }

    /**
     * 更新车辆信息
     */
    @PutMapping("/update/{id}")
    public ResponseResult<String> update(@PathVariable("id") Long id, @RequestBody Car car){
        car.setId(id);
        carService.update(car);
        return new ResponseResult<>(StatusCode.OK, "修改车辆信息成功");
    }

    /**
     * 删除车辆信息
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> delete(@PathVariable("id") Long id){
        carService.delete(id);
        return new ResponseResult<>(StatusCode.OK, "删除车辆信息成功");
    }

    /**
     * 多条件分页查询
     */
    @PostMapping("/search/{page}/{size}")
    public ResponseResult<Page<Car>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size,
                                                     @RequestBody Car car){
        Page<Car> carPage= carService.findPage(car, page, size);
        return new ResponseResult<>(StatusCode.OK, "查询成功", carPage);
    }

    /**
     * 根据id获取车辆信息
     */
    @GetMapping("/{id}")
    public ResponseResult<Car> findById(@PathVariable("id") Long id){
        Car car = carService.findById(id);
        return new ResponseResult<>(StatusCode.OK, "查询成功", car);
    }

    /**
     * 默认分页搜索车辆信息
     */
    @GetMapping("/search/{page}/{size}")
    public ResponseResult<Page<Car>> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Page<Car> carPage = carService.findPage(page, size);
        return new ResponseResult<>(StatusCode.OK,"查询成功", carPage);
    }

}
